package org.come.Jpanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

import javax.swing.BorderFactory;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTree;
import javax.swing.ScrollPaneConstants;
import javax.swing.SwingUtilities;
import javax.swing.event.TreeSelectionEvent;
import javax.swing.event.TreeSelectionListener;
import javax.swing.plaf.basic.BasicTreeUI;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeCellRenderer;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.TreePath;

import org.come.Frame.ZhuFrame;
import org.come.bean.Coordinates;
import org.come.mouslisten.Mouselistener;
import org.come.socket.Agreement;
import org.come.socket.SendMessageUntil;
import org.come.until.*;

import com.tool.ModerateTask.AllTask;
import com.tool.ModerateTask.Hero;
import com.tool.ModerateTask.Task;
import com.tool.ModerateTask.TaskData;
import com.tool.ModerateTask.TaskMixDeal;
import com.tool.ModerateTask.TaskProgress;
import com.tool.ModerateTask.TaskSet;
import com.tool.btn.FormsOnOffBtn;
import com.tool.btn.JpanelOnJalbelBtn;
import com.tool.tcpimg.InputBean;
import com.tool.tcpimg.RichLabel;
import com.tool.tcpimg.UIUtils;
import com.updateNew.MyIsif;

 

/**
 * @author 郑天宇    任务主面板
 * @date : 2017年11月20日 下午3:21:40
 */
public class TesttaskJapnel extends JPanel {
	private JScrollPane jScrollPane,//任务列表滚动条
			jScrollPane2;//任务介绍滚动条
	private JTree jTree; //任务列表
	private DefaultMutableTreeNode top; // 总节点

	private int caozuo;
	private int taskId;
	private RichLabel label;
	private JpanelOnJalbelBtn btnQX;
	private JpanelOnJalbelBtn btnZZ;
	private InputBean inputBean;
	public TesttaskJapnel() {
		this.caozuo = 0;
		if("水墨UI".equals(MyIsif.getStyle())) {

			// TODO Auto-generated constructor stub
			this.setPreferredSize(new Dimension(579, 409));
			this.setBackground(Color.BLACK);
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);
			
			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("inkImg/old/2/1.png", 1, 3);
			offBtn.setBounds(579 - 37, 10, 25, 25);
			this.add(offBtn);
					
			top = new DefaultMutableTreeNode();
			jTree = new JTree(top);
			jTree.setOpaque(false);
			jTree.putClientProperty("JTree.lineStyle", "None"); //去除jtree的线条
			((BasicTreeUI)jTree.getUI()).setLeftChildIndent(0); //设置父节点与子节点左对齐，并去除缩进
			DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer() {
				@Override
				public Component getTreeCellRendererComponent(JTree tree, Object value, 
						boolean selected, boolean expanded, boolean leaf,int row, boolean hasFocus) {
					super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
					// 判断是几级的叶子
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
					if (node.getUserObject() instanceof TaskData) {
						TaskData taskData = (TaskData) node.getUserObject();
						setText(taskData.getTaskName());
					}
					return this;
				}
			};
			cellRenderer.setLeafIcon(null); //设置叶子图标
			cellRenderer.setOpenIcon(new ImageIcon("inkImg/hongmu/2/expand.png")); //设置打开子节点图标
			cellRenderer.setClosedIcon(new ImageIcon("inkImg/hongmu/2/not_expanded.png")); //设置关闭子节点图标
			cellRenderer.setFont(new Font("宋体", Font.PLAIN, 14));// 设置字体.
			cellRenderer.setBackgroundNonSelectionColor(new Color(0,0,0,0));//设置非选定节点的背景色
			cellRenderer.setBackgroundSelectionColor(new Color(99,93,90));//设置节点在选中状态下的背景色
			cellRenderer.setBorderSelectionColor(new Color(0,0,0,0));//设置选中状态下节点边框的颜色
			cellRenderer.setTextNonSelectionColor(Color.white);//设置绘制未选中状态下节点文本的颜色
			cellRenderer.setTextSelectionColor(Color.white);//设置绘制选中状态下节点文本的颜色
			
			jTree.setCellRenderer(cellRenderer);
			jTree.setRootVisible(false); //隐藏根节点  
			jTree.setRowHeight(20); //设置节点行高
			//单击展开子节点
			jTree.addMouseListener(new MouseAdapter() { // 添加鼠标事件处理
				   @Override
				   public void mousePressed(MouseEvent e) {
					   DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
					   if (SwingUtilities.isLeftMouseButton(e)) { // 点击了鼠标左键
				         TreePath path = jTree.getSelectionPath(); 
				         if(path != null){  
				        	 if(jTree.isExpanded(path)){
				        		 jTree.collapsePath(path);// 关闭节点
				        	 }else{
				        		 jTree.expandPath(path);// 展开节点
				        	 }
				         }
				        	     
				      }   
				   }
			});
			jTree.addTreeSelectionListener(new TreeSelectionListener() {
				@Override
				public void valueChanged(TreeSelectionEvent e) {
					JTree tree = (JTree) e.getSource();
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
					if (node!=null&&node.isLeaf()) {//叶子节点
						taskMsg((TaskData)node.getUserObject());
					}
				}
			});
			//任务列表滚动条
			jScrollPane = new JScrollPane(jTree);
			jScrollPane.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane.getVerticalScrollBar().setUI(new SrcollPanelUI());
			jScrollPane.getVerticalScrollBar().setUnitIncrement(190);
			jScrollPane.getViewport().setOpaque(false);
			jScrollPane.setOpaque(false);
			jScrollPane.setBounds(50, 78, 215, 290);
			jScrollPane.setBorder(BorderFactory.createEmptyBorder());
			jScrollPane.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			this.add(jScrollPane);
			
			label=new RichLabel("", UIUtils.TEXT_FONT1, 287);
			label.addMouseListener(new MouseListener() {
				
				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					if (inputBean!=null) {
						Mouselistener.DJInputBean(inputBean);
						inputBean=null;
					}
				}
				
				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub
					
					inputBean=label.isMonitor(e.getX(), e.getY());
					if (inputBean!=null) {
						inputBean.setM(true);
					}
				}
				
				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
				
				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub
					
				}
			});
			//任务介绍滚动条
			jScrollPane2 = new JScrollPane(label);
			jScrollPane2.setVerticalScrollBarPolicy(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS);
			jScrollPane2.getVerticalScrollBar().setUI(new SrcollPanelUI());
			jScrollPane2.getVerticalScrollBar().setUnitIncrement(190);
			jScrollPane2.getViewport().setOpaque(false);
			jScrollPane2.setOpaque(false);
			jScrollPane2.setBounds(275, 78, 285, 290);
			jScrollPane2.setBorder(BorderFactory.createEmptyBorder());
			jScrollPane2.setHorizontalScrollBarPolicy(ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
			this.add(jScrollPane2);
			
			// 确认加点
			btnQX = new JpanelOnJalbelBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_BLACK, 10, "取消任务", this);
			btnQX.setBounds(440, 30, 99, 24);
	        this.add(btnQX);

			btnZZ = new JpanelOnJalbelBtn("skin/8100226/newbutton/B22.png", 1, UIUtils.COLOR_BLACK, 112, "任务追踪", this);
			btnZZ.setBounds(320, 30, 99, 24);
			this.add(btnZZ);
		}else{
			// TODO Auto-generated constructor stub
			this.setPreferredSize(new Dimension(556, 415));
			this.setBackground(Color.BLACK);
			this.setLayout(null);
			this.setBackground(UIUtils.Color_BACK);

			// 关闭按钮事件
			FormsOnOffBtn offBtn = new FormsOnOffBtn("skin/8100226/button/s74.png", 1, 3);
			offBtn.setBounds(556 - 25, 0, 25, 25);
			this.add(offBtn);

			top = new DefaultMutableTreeNode();
			jTree = new JTree(top);
			jTree.setOpaque(false);
			jTree.putClientProperty("JTree.lineStyle", "None"); //去除jtree的线条
			((BasicTreeUI)jTree.getUI()).setLeftChildIndent(0); //设置父节点与子节点左对齐，并去除缩进
			DefaultTreeCellRenderer cellRenderer = new DefaultTreeCellRenderer() {
				@Override
				public Component getTreeCellRendererComponent(JTree tree, Object value,
															  boolean selected, boolean expanded, boolean leaf,int row, boolean hasFocus) {
					super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
					// 判断是几级的叶子
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) value;
					if (node.getUserObject() instanceof TaskData) {
						TaskData taskData = (TaskData) node.getUserObject();
						setText(taskData.getTaskName());
					}
					return this;
				}
			};
			cellRenderer.setLeafIcon(null); //设置叶子图标
			cellRenderer.setOpenIcon(new ImageIcon("inkImg/hongmu/2/expand.png")); //设置打开子节点图标
			cellRenderer.setClosedIcon(new ImageIcon("inkImg/hongmu/2/not_expanded.png")); //设置关闭子节点图标
			cellRenderer.setFont(new Font("宋体", Font.PLAIN, 14));// 设置字体.
			cellRenderer.setBackgroundNonSelectionColor(new Color(0,0,0,0));//设置非选定节点的背景色
			cellRenderer.setBackgroundSelectionColor(new Color(99,93,90));//设置节点在选中状态下的背景色
			cellRenderer.setBorderSelectionColor(new Color(0,0,0,0));//设置选中状态下节点边框的颜色
			cellRenderer.setTextNonSelectionColor(Color.white);//设置绘制未选中状态下节点文本的颜色
			cellRenderer.setTextSelectionColor(Color.white);//设置绘制选中状态下节点文本的颜色

			jTree.setCellRenderer(cellRenderer);
			jTree.setRootVisible(false); //隐藏根节点
			jTree.setRowHeight(20); //设置节点行高
			//单击展开子节点
			jTree.addMouseListener(new MouseAdapter() { // 添加鼠标事件处理
				@Override
				public void mousePressed(MouseEvent e) {
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) jTree.getLastSelectedPathComponent();
					if (SwingUtilities.isLeftMouseButton(e)) { // 点击了鼠标左键
						TreePath path = jTree.getSelectionPath();
						if(path != null){
							if(jTree.isExpanded(path)){
								jTree.collapsePath(path);// 关闭节点
							}else{
								jTree.expandPath(path);// 展开节点
							}
						}

					}
				}
			});
			jTree.addTreeSelectionListener(new TreeSelectionListener() {
				@Override
				public void valueChanged(TreeSelectionEvent e) {
					JTree tree = (JTree) e.getSource();
					DefaultMutableTreeNode node = (DefaultMutableTreeNode) tree.getLastSelectedPathComponent();
					if (node!=null&&node.isLeaf()) {//叶子节点
						taskMsg((TaskData)node.getUserObject());
					}
				}
			});
			//任务列表滚动条

			jScrollPane = new JScrollPane(jTree);
			jScrollPane.setVerticalScrollBarPolicy(22);
			jScrollPane.getVerticalScrollBar().setUI(new ScrollUI());
			jScrollPane.getVerticalScrollBar().setUnitIncrement(190);
			jScrollPane.getViewport().setOpaque(false);
			jScrollPane.setOpaque(false);
			jScrollPane.setBounds(20, 59, 212, 290);
			jScrollPane.setBorder(BorderFactory.createEmptyBorder());
			jScrollPane.setHorizontalScrollBarPolicy(31);
			this.add(jScrollPane);

			label=new RichLabel("", UIUtils.TEXT_FONT1, 287);
			label.addMouseListener(new MouseListener() {

				@Override
				public void mouseReleased(MouseEvent e) {
					// TODO Auto-generated method stub
					if (inputBean!=null) {
						Mouselistener.DJInputBean(inputBean);
						inputBean=null;
					}
				}

				@Override
				public void mousePressed(MouseEvent e) {
					// TODO Auto-generated method stub

					inputBean=label.isMonitor(e.getX(), e.getY());
					if (inputBean!=null) {
						inputBean.setM(true);
					}
				}

				@Override
				public void mouseExited(MouseEvent e) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseEntered(MouseEvent e) {
					// TODO Auto-generated method stub

				}

				@Override
				public void mouseClicked(MouseEvent e) {
					// TODO Auto-generated method stub

				}
			});
			//任务介绍滚动条

			jScrollPane2 = new JScrollPane(label);
			jScrollPane2.setVerticalScrollBarPolicy(22);
			jScrollPane2.getVerticalScrollBar().setUI(new ScrollUI());
			jScrollPane2.getVerticalScrollBar().setUnitIncrement(190);
			jScrollPane2.getViewport().setOpaque(false);
			jScrollPane2.setOpaque(false);
			jScrollPane2.setBounds(247, 59, 280, 290);
			jScrollPane2.setBorder(BorderFactory.createEmptyBorder());
			jScrollPane2.setHorizontalScrollBarPolicy(31);
			this.add(jScrollPane2);

			// 确认加点 UIUtils.COLOR_BTNPUTONG, "脱离帮派", UIUtils.TEXT_HY88,
			btnQX = new JpanelOnJalbelBtn("inkImg/hongmu/1/1_png.button.btn_xy.png", 1, UIUtils.COLOR_BTNPUTONG2, 10, "取消任务", this);
			btnQX.setBounds(440, 360, 80, 30);
			this.add(btnQX);
			btnZZ = new JpanelOnJalbelBtn("inkImg/hongmu/1/1_png.button.btn_xy.png", 1,  UIUtils.COLOR_BTNPUTONG2, 112, "任务追踪", this);
			btnZZ.setBounds(340, 360, 80, 30);
			this.add(btnZZ);
		}
	}
	private ImageIcon icon;
	@Override
	protected void paintComponent(Graphics g) {
		super.paintComponent(g);
		if("水墨UI".equals(MyIsif.getStyle())) {

			// 任务栏
			if (icon==null){
				icon=new ImageIcon("inkImg/newbackground/B224.png");
			}
			g.drawImage(icon.getImage(), 0, 0, 579, 409, this);
		}else{
			// 任务栏
			if (icon==null){
				icon=new ImageIcon("inkImg/hongmu/1/rw1.png");
			}
			g.drawImage(icon.getImage(), 0, 0, 556, 415, this);
		}
	}
	private List<Integer> setList=new ArrayList<>();
	/**展示已领取的任务*/
	public void showTaskMethod(){
		top.removeAllChildren();
		setList.clear();//大到小
		AllTask allTask=UserMessUntil.getAllTask();
		Hero.getHero().getSetId(setList);
		allTask.getSetId(setList);
		for (int i = 0; i < allTask.getTypeList().size(); i++) {
			String taskType=allTask.getTypeList().get(i);
			DefaultMutableTreeNode node=null;
			for (int j = setList.size()-1;j>=0;j--) {
				TaskSet taskSet=UserMessUntil.getTaskSet(setList.get(j));
				if (taskSet.getTaskType().equals(taskType)) {
					setList.remove(j);
					Task task=Hero.getHero().getTaskSet(taskSet.getTaskSetID());
					if (task!=null) {
						if (node == null) {
							node = new DefaultMutableTreeNode(taskType);
						}
						DefaultMutableTreeNode treeNode=new DefaultMutableTreeNode(task.getTaskData());
						node.add(treeNode);
					}
				}
			}
			if (node!=null) {
				top.add(node);
			}
		}
		DefaultTreeModel treeModel = (DefaultTreeModel)jTree.getModel();
		treeModel.reload();

	}
	/**切换显示内容*/
	public void taskMsg(TaskData taskData){
		if (taskId==taskData.getTaskID()) {
			return;
		}
		taskShow(Hero.getHero().getTaskId(taskData.getTaskID()), taskData);
	}
	/**取消任务*/
	public void removeTask(){
		Task task=Hero.getHero().getTaskId(taskId);
		if (task==null) {
			ZhuFrame.getZhuJpanel().addPrompt2("选择要取消的任务");
			return;
		}
		if (Util.getTime()<TaskMixDeal.time+1000) {
			ZhuFrame.getZhuJpanel().addPrompt2("你需要等待1分钟才能再一次取消任务");
			return;
		}
		TaskMixDeal.time=Util.getTime();
		String mes = Agreement.getAgreement().TaskNAgreement("E"+task.getTaskId());
		SendMessageUntil.toServer(mes);
	}
	public void taskShow(Task task,TaskData taskData){
		if (taskData==null) {
			this.taskId=0;
			label.setTextSize("",287);
			return;
		}

		this.taskId=taskData.getTaskID();

		if (task!=null) {
			StringBuffer buffer=new StringBuffer();
			if (taskData.getTaskText()!=null) {
				buffer.append("#Y任务目的:#r#W");
				String text=taskData.getTaskText();
				int value1=-1;
				int value2=-1;
				int size=0;
				do {
					value1=text.indexOf("{");
					if (value1!=-1) {
						value2=text.indexOf("}");
					}
					if (value1==-1||value2==-1) {
						buffer.append(text);
					}else {
						buffer.append(text.substring(0, value1));
						String type=text.substring(value1+1, value2);
						if (type.startsWith("目标")) {
							if (size<task.getProgress().size()) {
								TaskProgress progress=task.getProgress().get(size);
								if (progress.getType()==0||progress.getType()==1||progress.getType()==2||progress.getType()==3) {
									if (progress.getMap()==0) {
										buffer.append(progress.getDName());
									}else {
										Coordinates coordinates=new Coordinates(progress.getMap(),progress.getX(),progress.getY());
										InputBean inputBean=new InputBean(null,progress.getType()+20,new BigDecimal(progress.getDId()),progress.getDName(),"G",coordinates);
										if (progress.getType()==1) {
											buffer.append("#G");
											buffer.append(UserMessUntil.getAllmapbean().getAllMap().get(progress.getMap()+"").getMapname());
											buffer.append("#W击杀");
										}
										buffer.append("#V");
										buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
										buffer.append("#L");
										buffer.append("(");
										buffer.append(progress.getX()/20);
										buffer.append(",");
										buffer.append(progress.getY()/20);
										buffer.append(")");
									}
								}else if (progress.getType()==4) {
									buffer.append("带着");
									buffer.append(progress.getMax());
									buffer.append("个");
									buffer.append(progress.getDName());
									buffer.append("探望");
									if (progress.getMap()==0) {
										buffer.append(progress.getGName());
									}else {
										Coordinates coordinates=new Coordinates(progress.getMap(),progress.getX(),progress.getY());
										InputBean inputBean=new InputBean(null,progress.getType()+20,new BigDecimal(progress.getGId()),progress.getGName(),"G",coordinates);
										buffer.append("#V");
										buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
										buffer.append("#L");
										buffer.append("(");
										buffer.append(progress.getX()/20);
										buffer.append(",");
										buffer.append(progress.getY()/20);
										buffer.append(")");
									}
								}else if (progress.getType()==5) {
									buffer.append("护送");
									buffer.append(progress.getDName());
									buffer.append("到");
									if (progress.getMap()==0) {
										buffer.append(progress.getGName());
									}else {
										Coordinates coordinates=new Coordinates(progress.getMap(),progress.getX(),progress.getY());
										InputBean inputBean=new InputBean(null,progress.getType()+20,new BigDecimal(progress.getGId()),progress.getGName(),"G",coordinates);
										buffer.append("#V");
										buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
										buffer.append("#L");
										buffer.append("(");
										buffer.append(progress.getX()/20);
										buffer.append(",");
										buffer.append(progress.getY()/20);
										buffer.append(")");
									}
								}
							}
							size++;
						}else if (type.startsWith("位置")) {
							String[] vs=type.split("-");
							Coordinates coordinates=new Coordinates(Integer.parseInt(vs[1]), Integer.parseInt(vs[2]), Integer.parseInt(vs[3]));
							InputBean inputBean=new InputBean(null,10,null,vs[1].substring(2),"G",coordinates);
							buffer.append("#V");
							buffer.append(GsonUtil.getGsonUtil().getgson().toJson(inputBean));
							buffer.append("#L");
							buffer.append("(");
							buffer.append(coordinates.getX()/20);
							buffer.append(",");
							buffer.append(coordinates.getY()/20);
							buffer.append(")");
						}
						text=text.substring(value2+1);
					}
				} while (value1!=-1&&value2!=-1);
			}
			if (task.getProgress().size()!=0) {
				if (buffer.length()!=0) {
					buffer.append("#r #r");
				}
				buffer.append("#Y任务状态:");
				for (int i = 0; i < task.getProgress().size(); i++) {
					TaskProgress progress=task.getProgress().get(i);
					buffer.append("#r#W");
					buffer.append(progress.getDName());
					buffer.append(":#G");
					buffer.append(progress.getSum());
					buffer.append("/");
					buffer.append(progress.getMax());
				}
			}
			TaskSet taskSet=UserMessUntil.getTaskSet(taskData.getTaskSetID());
			if (taskSet.getTaskMsg()!=null) {
				if (buffer.length()!=0) {
					buffer.append("#r #r");
				}
				buffer.append("#Y任务提示:#r#W");
				buffer.append(taskSet.getTaskMsg());
			}
			label.setTextSize(buffer.toString(),287);
		}
	}
	public int getTaskId() {
		return taskId;
	}

	public JpanelOnJalbelBtn getBtnZZ() {
		return this.btnZZ;
	}

	public void setBtnZZ(final JpanelOnJalbelBtn btnZZ) {
		this.btnZZ = btnZZ;
	}

	public int getCaozuo() {
		return this.caozuo;
	}

	public void setCaozuo(final int caozuo) {
		this.caozuo = caozuo;
	}
}
